import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:ywn_app/assembly/custom_swiper.dart';
import 'package:ywn_app/custom/MusicPlay.dart';
import 'package:ywn_app/moudle/entity/Images.dart';

class MovieCateGory extends StatefulWidget {
  @override
  _MovieCateGoryState createState() => _MovieCateGoryState();
}

class _MovieCateGoryState extends State<MovieCateGory> with AutomaticKeepAliveClientMixin {
  List musicList = [];
  List tuijianList = [];
  List singerList = [];
  List rebangList = [];
  List<Images> bannerList1=[];
  ScrollController _controller;

  void _pullNet() async {
    await http.get(
        'http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=93&pn=1&rn=30',
        headers: {
          'Cookie': 'kw_token=22CK84MY925',
          'csrf': '22CK84MY925'
        }).then((http.Response response) {
      Map<String, dynamic> user = json.decode(response.body);
      if (user['code'] == 200) {
        musicList = user['data']['musicList'];
      }
    });
  }

  void _tuiJianNet() async {
    await http.get(
        'http://www.kuwo.cn/api/www/rcm/index/playlist?id=rcm&pn=1&rn=5',
        headers: {
          'Cookie': 'kw_token=7J23VNPXENR',
          'csrf': '7J23VNPXENR'
        }).then((http.Response response) {
      Map<String, dynamic> user = json.decode(response.body);
      if (user['code'] == 200) {
        tuijianList = user['data']['list'];
      }
    });
  }

  void _pullImages() async {
    await http.get(
        'http://www.9sky.com/main/banner/list').then((http.Response response) {
      Map<String, dynamic> user = json.decode(response.body);
      if ("0000" == user['code']) {
        List list = user['data']['list'];
        list.forEach((element) {
          Images image = new Images();
          image.imagesUrl = element['image_url'];
          image.title = element['title'];
          bannerList1.add(image);
        });
      }
    });
  }

  void _pullSinger() async {
    await http.get(
        'http://www.kuwo.cn/api/www/artist/artistInfo?category=11&pn=1&rn=8',
        headers: {
          'Cookie': 'kw_token=7J23VNPXENR',
          'csrf': '7J23VNPXENR'
        }).then((http.Response response) {
      Map<String, dynamic> user = json.decode(response.body);
      if (user['code'] == 200) {
        singerList = user['data']['artistList'];
      }
    });
  }

  void _rebang() async {
    await http.get(
        'http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=16&pn=1&rn=30',
        headers: {
          'Cookie': 'kw_token=7J23VNPXENR',
          'csrf': '7J23VNPXENR'
        }).then((http.Response response) {
      Map<String, dynamic> user = json.decode(response.body);
      if (user['code'] == 200) {
        rebangList = user['data']['musicList'];
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
    _pullImages();
    _pullNet();
    _tuiJianNet();
    _pullSinger();
    _rebang();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      controller: _controller,
      child: Column(
        children: <Widget>[
          CustomSwiper(images: bannerList1),
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(top:10,left: 10,right: 10,bottom: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text('推荐歌单',style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold
                ),),
                GestureDetector(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text('更多',style: TextStyle(
                        fontSize: 12,
                      ),),
                      Icon(Icons.chevron_right,size: 18,color: Colors.grey[600],)
                    ],
                  ),
                  onTap: (){},
                ),
              ],
            )
          ),
          GridView.builder(
            scrollDirection: Axis.vertical,
            controller: _controller,
            shrinkWrap: true,
            itemCount: 6,
            padding: EdgeInsets.only(top:5,left: 10,right: 10,bottom: 5),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 4,
                crossAxisSpacing: 5,
                childAspectRatio: .7
            ),
            itemBuilder: (BuildContext context, int index) {
              return tuijianList.length>0?Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Stack(
                    children: <Widget>[
                      ClipRRect(
                        borderRadius: BorderRadius.circular(6.0),
                        child: new Image.network(tuijianList[index]['img'],fit: BoxFit.cover,),
                      ),
                      Positioned(
                        bottom: 0,
                        left: 0,
                        right:0,
                        child: Container(
                          width: 500,
                          child: Row(
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.only(right:2),
                                child: Icon(Icons.play_circle_outline,size: 16,color: Colors.white,),
                              ),
                              Text('${NumUtil.getNumByValueStr(NumUtil.divide(tuijianList[index]['listencnt'],10000).toString(),fractionDigits: 1)}万',
                                style: TextStyle(color: Colors.white,fontSize: 12))
                            ],
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.center,
                          ),
                          padding: EdgeInsets.all(5),
                          decoration: BoxDecoration(
                              gradient: LinearGradient(
                                begin: Alignment.bottomCenter,
                                end: Alignment.topCenter,
                                colors: [Colors.black54, Colors.black38,Colors.transparent],
                              ),
                            borderRadius: BorderRadius.only(bottomRight: Radius.circular(6),bottomLeft: Radius.circular(6))
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 5,),
                  Text(tuijianList[index]['name'],
                    overflow: TextOverflow.ellipsis,maxLines: 2,
                    style: TextStyle(fontSize: 12),),
                ],
              ):SizedBox.shrink();
            },
          ),
          Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 10,right: 10,bottom: 5),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('歌手推荐',style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold
                  ),),
                  GestureDetector(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('更多',style: TextStyle(
                          fontSize: 12,
                        ),),
                        Icon(Icons.chevron_right,size: 16,color: Colors.grey[600],)
                      ],
                    ),
                    onTap: (){},
                  ),
                ],
              )
          ),
          GridView.builder(
            shrinkWrap: true,
            padding: EdgeInsets.only(left: 10,right: 10,bottom: 5),
            controller: _controller,
            itemCount: singerList.length,
            itemBuilder: (BuildContext context, int index){
              return singerList.length>0?Column(
                children: <Widget>[
                  ClipOval(
                    child: Image.network(singerList[index]['pic'],fit: BoxFit.cover,),
                  ),
                  Text(singerList[index]['name'],maxLines: 1,overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                  Text('${singerList[index]['musicNum']}首音乐',maxLines: 1,overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.grey
                    ),
                  ),
                ],
              ):SizedBox.shrink();
            },
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                mainAxisSpacing: 2,
                crossAxisSpacing: 4,
                childAspectRatio: .6
            ),
          ),
          Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 10,right: 10,bottom: 5),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('热歌排行',style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold
                  ),),
                  GestureDetector(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('更多',style: TextStyle(
                          fontSize: 12,
                        ),),
                        Icon(Icons.chevron_right,size: 16,color: Colors.grey[600],)
                      ],
                    ),
                    onTap: (){},
                  ),
                ],
              )
          ),
          ListView.separated(
            padding: EdgeInsets.only(left: 10,right: 10,bottom: 10),
            shrinkWrap: true,
            controller: _controller,
            itemCount: rebangList.length,
            itemBuilder: (BuildContext context, int index) {
              return InkWell(
                onTap: (){
                  Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => MusicPlay(
                    rid: rebangList[index]['rid'].toString(),
                  )));
                },
                child: Container(
                  height: 50,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      ClipRRect(
                        borderRadius: BorderRadius.circular(6.0),
                        child: new Image.network(rebangList[index]['pic'],fit: BoxFit.cover),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Container(
                        width: MediaQuery.of(context).size.width-120,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(rebangList[index]['name'],
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            Text('${rebangList[index]['artist']} - ${rebangList[index]['name']}',style: TextStyle(
                                fontSize: 12,
                                color: Colors.grey
                            ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ],
                        ),
                      ),
                      Text(rebangList[index]['songTimeMinutes'],
                        style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[500]
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return SizedBox(
                height: 8,
              );
          },)
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
